package com.honey.mybatis.generator.mappper;

import java.util.List;

import com.honey.compilation.xml.XmlElement;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.introspect.IntrospectTableColumn;
import com.honey.core.dbmapping.structure.Schema;
import com.honey.core.dbmapping.structure.Table;
import com.honey.core.dbmapping.structure.TableColumn;
import com.honey.mybatis.generator.MybatisPlugin;

public class BlobsResultMap extends AbstractMybatisMapper {

	public BlobsResultMap(MybatisPlugin belongPlugin) {
		super(belongPlugin);
	}
	
	public void appendElement(XmlElement parent, Schema schema, IntrospectSchemaColumn introspectSchemaColumn ){
		String entityName =  super.getEntityPrimitiveClassName(schema);
		String entityPackage =  super.getBlobEntityClassType(schema).getFullyQualifiedName();
		String modelPackage =  super.getModelPrimitiveClassType(schema).getFullyQualifiedName();
		parent.addNewLine();
		parent.addComments("大字段结果集的映射");
		XmlElement element = new XmlElement(parent,"resultMap")
		.addAttribute("id",entityName+mybatisPlugin.getBlobSuffix())
		.addAttribute("type",entityPackage)
		.addAttribute("extends",entityName)
		;  
		if( schema instanceof Table ){
			IntrospectTableColumn introspectTableColumn = (IntrospectTableColumn)introspectSchemaColumn;
			List<TableColumn> list =  introspectTableColumn.blobColumns();
			for(TableColumn column : list){
				XmlElement result= new XmlElement(element,"result")
				.addAttribute("column", column.getName())
				.addAttribute("property", super.getFieldName(column))
				.addAttribute("jdbcType", JdbcTypeName.getJdbcTypeName(column.getJdbcType()))
				;
				if( column.getEnumSchema() != null){
					result.addAttribute("typeHandler", modelPackage);
					result.addAttribute("javaType", modelPackage+"$"+super.getStateEnumName(column));
				}
			}
		}
	}
}
